<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/4 13:49
 * Modified Time: 2019/7/4 13:49
 * Version: 2.0
 */

namespace App\Modules\Inventory\Criterias;


use App\Exceptions\AllValidatorException;
use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;
use Flower\Core\Repository\Exceptions\ValidatorException;
use function foo\func;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Facades\DB;

class GetGoodsListCriteria extends BaseCriterias
{
    protected $store_id;
    protected $keywords;
    protected $f_class;
    protected $s_class;

    public function __construct($store_id, $keywords, $f_class, $s_class)
    {
        $this->store_id = $store_id;
        $this->keywords = $keywords;
        $this->f_class = $f_class;
        $this->s_class = $s_class;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $store_id = $this->store_id;
        $keywords = $this->keywords;
        $f_class = $this->f_class;
        $s_class = $this->s_class;

        $class_select = [
            'goods_category.id',
            'goods_category.store_id',
            'goods_category.parent_id',
            'goods_category.name',
            'goods_category.status',
            'goods_category.created_at'
        ];

        return $model
            ->select($class_select)
            ->with(['parent' => function ($query) {
                $query->select(['id', 'name']);
            }])
            ->with(['goods' => function ($query) use ($keywords) {
                $query->select([
                    'goods.id',
                    'goods.store_id',
                    'cat_id',
                    'name',
                    'small_name',
                    'buying_price',
                    'market_price',
                    'royalty',
                    'is_sell',
                    'specs',
                    'volume',
                    'image',
                    'status',
                    'goods.created_at'
                ])
                    ->with(['image' => function ($query) {
                        $query->select(['id', 'image']);
                    }])
                    ->with(['goodsStock' => function ($query) {
                        $select = [
                            'id',
                            'goods_id',
                            'round((hx_goods_stock.percent + prev_percent)/100,0) as count'
                        ];
                        $select = implode($select, ',');
                        $query->selectRaw($select);
                    }])
                    ->leftJoin('goods_stock', 'goods.id', 'goods_stock.goods_id')
                    ->when($keywords, function ($query) use ($keywords) {
                        $query->where('goods.name', 'like', '%' . $keywords . '%');
                    });
            }])->leftJoin('goods_category as parent_category', 'goods_category.parent_id', 'parent_category.id')
            ->when($f_class, function ($query) use ($f_class) {
                $query->where('goods_category.parent_id', $f_class);
            })
            ->when($s_class, function ($query) use ($s_class) {
                $query->where('goods_category.id', $s_class);
            })
            ->where('goods_category.store_id', $store_id)
            ->where('goods_category.parent_id', '<>', 0)
            ->orderBy('id', 'DESC');

    }
}